import axios from 'axios'
import {urls} from "@/config";
import {ElMessage} from "element-plus";

const request = axios.create({
    baseURL: urls,
    timeout: 10000
})

// 添加请求和响应拦截器
request.interceptors.request.use((config) => {
    return config
})

//  添加响应拦截器
request.interceptors.response.use((response) => {
    // console.log(65, response.data)
    return response.data
}, (error) => {
    let message = ''
    const status = error.response.code
    switch (status) {
        case 401:
            message = 'token过期'
            break
        case 403:
            message = '无权访问'
            break
        case 404:
            message = '请求地址错误'
            break
        case 500:
            message = '服务器出现戳五'
            break
        default:
            message = '网路出现问题'
    }
    ElMessage({
        type: 'error',
        message
    })
    return Promise.reject(error)
})

export default request
